package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kejiguan.ui.adapter.ModifyExhiitionNeAdapter;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyNeItemDialog;
import com.kejiguan.ui.dialog.ModifyNeItemDialogUtil;
import com.kejiguan.ui.model.ExhibitionNeItem;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.presenter.ModifyExhibitionPresenter;
import com.kejiguan.ui.response.ExhibitionModifyResponse;
import com.kejiguan.ui.response.ExhibitionQueryNeItemsResponse;
import com.kejiguan.ui.response.ExihibitionRemoveNeItemResponse;
import com.kejiguan.ui.response.NeItemManagerResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by YYY on 16/7/19.
 */
public class ModifyExhibitionActivity extends Activity implements View.OnClickListener, ModifyNeItemDialog.OnNeItemModifiedListener{

    private ModifyExhibitionPresenter mPresenter;

    private TextView exhibitionIdView;
    private EditText exhibitionNameInput;
    private RecyclerView neListView;
    private Button saveBtn;
    private Button addBtn;
    private Button backBtn;
    private ModifyExhiitionNeAdapter neAdapter;

    private ExhibitionNeItem exhibitionNeItem;
    private NeItem removeNeItem;
    private NeItem modifyNeItem;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        if (null==exhibitionNeItem)
        {
            finish();
        }
        EventBus.getDefault().register(this);
        mPresenter=new ModifyExhibitionPresenter();
        mPresenter.onCreate(ModifyExhibitionActivity.this);
        setContentView(R.layout.activity_modify_exhibition_layout);

        exhibitionIdView= (TextView) findViewById(R.id.exhibition_id);
        exhibitionNameInput= (EditText) findViewById(R.id.exhibition_name_input);
        neListView= (RecyclerView) findViewById(R.id.ne_list);
        saveBtn= (Button) findViewById(R.id.save_btn);
        addBtn= (Button) findViewById(R.id.add_btn);
        backBtn= (Button) findViewById(R.id.back_btn);
        saveBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        backBtn.setOnClickListener(this);
        exhibitionIdView.setText(exhibitionNeItem.getExhibitionId());
        exhibitionNameInput.setText(exhibitionNeItem.getExhibitionName());

        LinearLayoutManager layoutManager=new LinearLayoutManager(ModifyExhibitionActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        neListView.setLayoutManager(layoutManager);
        neAdapter=new ModifyExhiitionNeAdapter(ModifyExhibitionActivity.this,null);
        neAdapter.setOnClickListener(ModifyExhibitionActivity.this);
        neListView.setAdapter(neAdapter);

        mPresenter.queryNeItemByWpaId(exhibitionNeItem.getExhibitionId());
    }

    private void getIntentData()
    {
        exhibitionNeItem= (ExhibitionNeItem) getIntent().getSerializableExtra("exhibition");
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.back_btn:
                finish();
                break;
            case R.id.add_btn:
                Intent intent=new Intent(ModifyExhibitionActivity.this,ChooseNeItemToExhibitionActivity.class);
                intent.putExtra("exhibitionId",exhibitionNeItem.getExhibitionId());
                intent.putExtra("exhibitionName",exhibitionNeItem.getExhibitionName());
                startActivity(intent);
                break;
            case R.id.remove_btn:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                removeNeItem=neAdapter.getNeItemByPosition(position);
                CommDialogUtil.getInstance().show(ModifyExhibitionActivity.this,"将网元"+removeNeItem.getNeName()+"从展项"+exhibitionNeItem.getExhibitionName()+"移除?",ModifyExhibitionActivity.this,"移除","取消",0);
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                mPresenter.removeNeItem(removeNeItem);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
            case R.id.save_btn:
                String exhiitionName=exhibitionNameInput.getText().toString().trim();
                if (TextUtils.isEmpty(exhiitionName))
                {
                    Toast.makeText(ModifyExhibitionActivity.this,"展项名称不能为空!",Toast.LENGTH_SHORT).show();
                }else
                {
                    if (!exhiitionName.equals(exhibitionNeItem.getExhibitionName()))
                    {
                        mPresenter.modifyExhibitionName(exhibitionNeItem,exhiitionName);
                    }else
                    {
                        finish();
                    }
                }
                break;
            case R.id.detail_view:
//                int modifyPosition= (int) v.getTag(R.id.recyclerview_item_position);
//                modifyNeItem=neAdapter.getNeItemByPosition(modifyPosition);
//                ModifyNeItemDialogUtil.getInstance().show(ModifyExhibitionActivity.this,modifyNeItem,ModifyExhibitionActivity.this);
                break;
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ExhibitionQueryNeItemsResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            neAdapter.setNeItems(response.getNeList());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ExihibitionRemoveNeItemResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            neAdapter.removeNeItem(response.getRemovedNeItem());
        }

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemManagerResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            switch (response.getOperType())
            {
                case "attach":
                    neAdapter.addNeItem(response.getNeItem());
                    break;
                case "mod":
                    for (int i=0;i<neAdapter.getItemCount();i++)
                    {
                        if (neAdapter.getNeItemByPosition(i).getId().equals(response.getNeItem().getId()))
                        {
                            neAdapter.setNeItem(i,response.getNeItem());
                            break;
                        }
                    }
                    break;
            }

        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ExhibitionModifyResponse response) {

        if ("0000".equals(response.getRetCode()))
        {
            finish();
        }

    }

    @Override
    public void neItemModifiedListener(NeItem item) {

        mPresenter.modifyNeItem(item);
    }
}
